package com.cskaoyan.controller;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.bo.GoodsCreateBo;
import com.cskaoyan.bean.vo.CarAndBrandVo;
import com.cskaoyan.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;

    @GetMapping("list")
    public BaseRespVo list(Integer page, Integer limit, String sort, String order, String goodsSn, String name) {
        ListData<Goods> listData = goodsService.list(page, limit, sort, order, goodsSn, name);
        return BaseRespVo.ok(listData);
    }


    @PostMapping("delete")
    public BaseRespVo detail(@RequestBody Goods goods) {
        goodsService.delete(goods);
        return BaseRespVo.ok();
    }

    @GetMapping("catAndBrand")
    public BaseRespVo carAndBrand() {
        Map<String, List<CarAndBrandVo>> map = goodsService.carAndBrand();
        return BaseRespVo.ok(map);
    }

    @GetMapping("detail")
    public BaseRespVo detail(Integer id) {
        Map<String, Object> map = goodsService.detail(id);
        return BaseRespVo.ok(map);
    }

    //todo
    @PostMapping("create")
    public BaseRespVo create(@RequestBody GoodsCreateBo goodsCreateBo) {
        Goods goods = goodsCreateBo.getGoods();
        List<GoodsAttribute> attributes = goodsCreateBo.getAttributes();
        List<GoodsProduct> products = goodsCreateBo.getProducts();
        List<GoodsSpecification> specifications = goodsCreateBo.getSpecifications();

        goodsService.create(goods,attributes,products,specifications);

        return BaseRespVo.ok();
    }

    @PostMapping("update")
    public BaseRespVo update(@RequestBody GoodsCreateBo goodsCreateBo){
        Goods goods = goodsCreateBo.getGoods();
        List<GoodsAttribute> attributes = goodsCreateBo.getAttributes();
        List<GoodsProduct> products = goodsCreateBo.getProducts();
        List<GoodsSpecification> specifications = goodsCreateBo.getSpecifications();

        goodsService.update(goods,attributes,products,specifications);

        return BaseRespVo.ok();
    }

}
